import axios from 'axios';

const api = axios.create({
  baseURL: '/api',
  withCredentials: true,
});

// 话题相关
export const fetchTopics = () => api.get('/topics');
export const fetchTopicDetail = (topicId: string) => api.get(`/topics/${topicId}`);
export const createTopic = (data: { name: string; description: string }) => api.post('/topics', data);
export const updateTopic = (topicId: string, data: { name?: string; description?: string }) => api.patch(`/topics/${topicId}`, data);
export const deleteTopic = (topicId: string) => api.delete(`/topics/${topicId}`);

// 帖子相关
export const fetchPosts = (topicId?: string) => topicId ? api.get(`/posts?topicId=${topicId}`) : api.get('/posts');
export const fetchPostDetail = (postId: string) => api.get(`/posts/${postId}`);
export const createPost = (data: { title: string; content: string; topicId: string }) => api.post('/posts', data);
export const updatePost = (postId: string, data: { title?: string; content?: string; topicId?: string }) => api.patch(`/posts/${postId}`, data);
export const deletePost = (postId: string) => api.delete(`/posts/${postId}`);

// 评论相关
export const fetchComments = (postId?: string) => postId ? api.get(`/comments?postId=${postId}`) : api.get('/comments');
export const fetchCommentDetail = (commentId: string) => api.get(`/comments/${commentId}`);
export const createComment = (data: { content: string; postId: string; parentId?: string }) => api.post('/comments', data);
export const updateComment = (commentId: string, data: { content?: string }) => api.patch(`/comments/${commentId}`, data);
export const deleteComment = (commentId: string) => api.delete(`/comments/${commentId}`);

export default api;
